package com.xtoad.ecms.baseinfo.controller;

import com.xtoad.ecms.baseinfo.dto.UserDTO;
import com.xtoad.ecms.baseinfo.service.IUserService;
import com.xtoad.ecms.common.web.annotation.RequiredToken;
import com.xtoad.ecms.common.web.annotation.ResponseAdvice;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户控制器
 *
 * @author xtoad
 * @date 2020/05/29
 */
@RestController
@RequestMapping("user")
@ResponseAdvice
@RequiredToken
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 根据id查询用户信息
     *
     * @param id 用户id
     * @return 查询结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public UserDTO getUserById(@PathVariable final Long id) {
        UserDTO user = userService.getUserById(id);
        user.setPassword(null);
        return user;
    }

    /**
     * 查询全部用户信息，不分页
     *
     * @return 全部用户信息
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<UserDTO> getAllUser() {
        List<UserDTO> users = userService.getAllUser();
        users.forEach(user -> user.setPassword(null));
        return users;
    }

    /**
     * 根据姓名模糊查询用户信息
     *
     * @param name 姓名
     * @return 查询结果
     */
    @RequestMapping(value = "/getUserByName/{name}", method = RequestMethod.GET)
    public List<UserDTO> getUserWithName(@PathVariable final String name) {
        List<UserDTO> users = userService.getUserWithName(name);
        users.forEach(user -> user.setPassword(null));
        return users;
    }

    /**
     * 新增用户
     *
     * @param userDTO 用户信息
     * @return 更新结果
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public UserDTO insertUser(@Valid @RequestBody final UserDTO userDTO) {
        UserDTO insUser = userService.insert(userDTO);
        insUser.setPassword(null);
        return insUser;
    }

    /**
     * 更新用户信息
     *
     * @param userDTO 更新内容
     * @return 更新结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public UserDTO updateUser(@PathVariable final Long id, @Valid @RequestBody final UserDTO userDTO) {
        userDTO.setId(id);
        UserDTO updUser = userService.update(userDTO);
        updUser.setPassword(null);
        return updUser;
    }

    /**
     * 删除用户信息
     *
     * @param id 删除对象id
     * @return 结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Boolean deleteUser(@PathVariable final Long id) {
        userService.deleteById(id);

        return true;
    }


    /**
     * 批量新增
     *
     * @param users 用户信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/add", method = RequestMethod.POST)
    public List<UserDTO> batchAddUser(@Valid @RequestBody final List<UserDTO> users) {
        List<UserDTO> insUsers = userService.batchInsert(users);
        insUsers.forEach(user -> user.setPassword(null));
        return insUsers;
    }

    /**
     * 批量更新
     *
     * @param users 用户信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/modify", method = RequestMethod.POST)
    public List<UserDTO> batchUpdUser(@Valid @RequestBody final List<UserDTO> users) {
        List<UserDTO> updUsers = userService.batchUpdate(users);
        updUsers.forEach(user->user.setPassword(null));
        return updUsers;
    }

    /**
     * 批量删除
     *
     * @param users 用户信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/delete", method = RequestMethod.POST)
    public Boolean batchDelUser(@Valid @RequestBody final List<UserDTO> users) {
        userService.batchDelete(users);

        return true;
    }

}
